package com.project.controller;

import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import com.project.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("link")
public class LinkController {
    @Autowired
    private LinkService service;
    @RequestMapping("cutAll")
    public String cutAll(@RequestParam(value = "pageNo",defaultValue = "1")int pageNo){
//        调用业务组件方法得到结果
        CutPageBean<LinkBean>cutBean = service.cutAll(pageNo);
        String str = "<table border='1' width='80%'>";
        str+="<thead><tr><th>姓名</th><th>生日</th><th>电话</th></tr></thead>";
        str+="<tbody>";
        for (LinkBean link : cutBean.getList()){
            str+="<tr><td>"+link.getName()+"</td><td>"+link.getBirthday()+"</td>"+
                    "<td>"+link.getPhone()+"</td><td><a href='del?id="+link.getId()+"'>删除</a></td>" +
                    "<td><a href='update?id="+link.getId()+"'>修改</a></td></tr>";
        }
        str+="</tbody></table>";
        for (int i=1;i<=cutBean.getToalPage();i++){
            str+="<a href='cutAll?pageNo="+i+"'>"+i+"</a>";
        }
        str+="<a href='/addd.html'>添加</a>";
        return str;
    }
    @RequestMapping("del")
    public String del(int id){
        service.del(id);
        return this.cutAll(1);
    }
//    修改页面
    @RequestMapping("update")
    public String update(int id){
        LinkBean link = service.byId(id);
         String str = "<form action= updates method=\"post\">\n" +
                 "    <input type=\"hidden\" name=id value="+link.getId()+"><br>\n" +
                 "    名字："+link.getName()+"<br>" +
                 "    生日："+link.getBirthday()+"<br>" +
                 "    电话：<input type=\"text\" name=phone value="+link.getPhone()+"><br>\n" +
                 "    <input type=\"submit\" value=\"确定\">\n" +
                 "</form>";
        return str;
    }

    @RequestMapping(value = "updates",method = RequestMethod.POST)
    public String updates(int id,String phone){
        service.update(id,phone);
        return this.cutAll(1);
    }

    @RequestMapping(value = "/addd",method = RequestMethod.POST)
    public String addd(LinkBean link){
        service.add(link);
        return this.cutAll(1);
    }
}
